package model;

import java.util.*;


import sql.SqlConnector;

/**
 * 08-01-2010
 * 
 * This is the class in charge of all the appointment information
 * When an appointment has been added/changed/deleted it will
 * notify all observers
 * Currently there is no abstract class to define the storage
 * of the data, it only accepts the sqlconnector class.
 * 
 * @author Stijn Kliemesch
 */
public class AppointmentModel extends Observable {
	
	private SqlConnector connector;
	
	/**
	 * Constructor, needs to know the SqlConnector for the data
	 * storage and manupulation.
	 * 
	 * @param newConnector
	 */
	public AppointmentModel(SqlConnector newConnector) {
		connector = newConnector;
	}
	
	/**
	 * returns the requested appointment.
	 * 
	 * @param id The ID of the existing appointment
	 * @throws nullpointerexception on unexisting appointment ID
	 * @return AppointmentDataModel the selected appointment
	 */
	public AppointmentDataModel getAppointment(int id) {
		if(connector.appointmentExists(id)) {
			return connector.selectAppointment(id);
		} else {
			return null;
		}
	}
	
	/**
	 * Return all appointments from database.
	 * 
	 * @return Vector<AppointmentDataModel>
	 */
	public Vector<AppointmentDataModel> getAllAppointments() {
		return connector.selectAllAppointments();
	}
	
	/**
	 * Return all appointments from database between startDateRange and endDateRange.
	 * 
	 * @param startDateRange Start date range
	 * @param endDateRange End date range
	 * @return Vector<AppointmentDataModel>
	 */
	public Vector<AppointmentDataModel> getAllAppointments(Calendar startDateRange, Calendar endDateRange) {
		return connector.selectAllAppointmentsInRange(startDateRange, endDateRange);
	}
	
	/**
	 * Adds a new appointment to the database
	 * The start and end string should be formatted as follows:
	 * YYYY-MM-DD HH:MM
	 * 	
	 * @param appointmentDataModel
	 */
	public void addAppointment(AppointmentDataModel appointmentDataModel) {
		connector.addAppointment(appointmentDataModel);
		
		setChanged();
		notifyObservers();
	}
	
	/**
	 * Edits an existing appointment in the database
	 * The start and end string should be formatted as follows:
	 * YYYY-MM-DD HH:MM
	 * 	
	 * @param appointmentDataModel 
	 * @throws nullpointerexception on unexisting appointment ID
	 */
	public void editAppointment(AppointmentDataModel appointmentDataModel) {
		if(connector.appointmentExists(appointmentDataModel.getId())) {
			connector.updateAppointment(appointmentDataModel);
			
			setChanged();
			notifyObservers();
		} else {
			throw new NullPointerException();
		}
	}
	
	/**
	 * Deletes an existing appointment in the database.
	 * 
	 * @param id
	 * @throws nullpointerexception on unexisting appointment ID
	 */
	public void deleteAppointment(int id) {
		if(connector.appointmentExists(id)) {
			connector.deleteAppointment(id);
			setChanged();
			notifyObservers();
		} else {
			throw new NullPointerException();
		}
	}
	
	/**
	 * Gets the appointments that containt the search key
	 * 
	 * @param query
	 */
	public Vector<AppointmentDataModel> searchAppointments(String query) {
		return connector.search(query);
	}
	
	/**
	 * Gets the specified category.
	 * 
	 * @param id The id of the category
	 * @return CategoryDataModel the selected category
	 */
	public CategoryDataModel getCategory(int id) {
		return connector.selectCategory(id);
	}
	
	/**
	 * Gets all the categories.
	 * @return the entire list of categories from the connector.
	 */
	public Vector<CategoryDataModel> getAllCategories() {
		return connector.selectAllCategories();
	}
}